package com.czl.base.push

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.Utils
import com.umeng.message.entity.UMessage
import org.json.JSONException
import org.json.JSONObject


class NotificationClickReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action
        val message = intent.getStringExtra("msg")
        if (StringUtils.isEmpty(message))
            return
        try {
            val msg = UMessage(JSONObject(message))
            LogUtils.e("NotifyReceiver", "onReceive: action = $action==msg==$message")
            when (action) {
                DOWN_ACTION -> if ("go_app" == msg.after_open) { //打开应用
                    AppUtils.launchApp(Utils.getApp().packageName)
                } else if ("go_custom" == msg.after_open) { //自定义行为
//                    PublicStartMethod.go_custom(msg.custom, context)
                }
                CLOSE_ACTION ->                     //处理第2个按钮事件，关闭通知
                    LogUtils.d("=======", "CLOSE_ACTION")
                else -> {}
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }

    companion object {
        const val DOWN_ACTION = "DownClick"
        const val CLOSE_ACTION = "CloseClick"
    }
}